﻿#include "formiso.h"
#include "ui_formiso.h"
#include <QMenu.h>
#include "dlgadvancesetting.h"
#include "qwininterface.h"
#include <QMessageBox.h>
#include "formdefaultstyle.h"
#include <QtWidgets/QComboBox>
#include "Device.h"
#include <QFileDialog>
#include <QTextCodec>

FormISO::FormISO(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormISO),
    pop_menu(NULL)
{
    ui->setupUi(this);
    setStyleSheet("QPushButton{font:bold 12px \"Microsoft YaHei\";color: rgb(120, 120, 120);}"
                  "QPushButton::hover{font:bold 12px \"Microsoft YaHei\";color: rgb(80, 80, 80);border: 1px solid gray;}"
                  "QPushButton::pressed{font:bold 12px \"Microsoft YaHei\";color: rgb(80, 80, 80);border: 1px solid gray;}"
                  "QPushButton::checked{font:bold 12px \"Microsoft YaHei\";color: rgb(80, 80, 80);border: 1px solid green;}"
                  "QLabel{font:bold 14px \"Microsoft YaHei\";color: rgb(120, 120, 120);}");
    ui->pushButton_start_make->setStyleSheet("QPushButton{border-image: url(:/images/1_1_Btn1_1.jpg);}"
                                             "QPushButton:hover{border-image: url(:/images/1_1_Btn1_2.jpg);}"
                                             "QPushButton:pressed{border-image: url(:/images/1_1_Btn1_3.jpg);}");
    ui->pushButton_fire_cd->setStyleSheet("QPushButton{border-image: url(:/images/1_2_btn4_1.jpg);}"
                                          "QPushButton:hover{border-image: url(:/images/1_2_Btn4_2.jpg);}"
                                          "QPushButton:pressed{border-image: url(:/images/1_2_Btn4_3.jpg);}");
    ui->pushButton_simulate_boot->setStyleSheet("QPushButton{border-image: url(:/images/1_2_btn5_1.jpg);}"
                                                "QPushButton:hover{border-image: url(:/images/1_2_btn5_2.jpg);}"
                                                "QPushButton:pressed{border-image: url(:/images/1_2_btn5_3.jpg);}");
    ui->pushButton_more->setStyleSheet("QPushButton{border-image:url(:/images/1_more_1.jpg);}"
        "QPushButton:hover{border-image:url(:/images/1_more_2.jpg);}"
        "QPushButton:pressed{border-image: url(:/images/1_more_3.jpg);}");
    //创建菜单、菜单项
    pop_menu = new QMenu();
    QAction* add_images_action = new QAction(this);
    QAction* add_folder_action = new QAction(this);
    QAction* remove_selected_action = new QAction(this);
    add_images_action->setIcon(QIcon(":/images/2_Btn1_1.jpg"));
    add_images_action->setText(QString::fromLocal8Bit("微软官网下载"));
    add_images_action->setEnabled(false);
    add_folder_action->setIcon(QIcon(":/images/2_Btn2_1.jpg"));
    add_folder_action->setText(QString::fromLocal8Bit("官网下载"));
    add_folder_action->setEnabled(false);
    remove_selected_action->setIcon(QIcon(":/images/2_Btn3_1.jpg"));
    remove_selected_action->setText(QString::fromLocal8Bit("选择"));
    connect(remove_selected_action, SIGNAL(triggered()), this, SLOT(remove_selected_action_clicked()));
    connect(add_folder_action, SIGNAL(triggered()), this, SLOT(add_folder_action_clicked()));
    
    //清除原有菜单
    pop_menu->addAction(remove_selected_action);
    pop_menu->addAction(add_folder_action);
    pop_menu->addAction(add_images_action);
    ui->pushButton_more->setMenu(pop_menu);
    selectOS = new SelectOS((QWidget*)this->parent());
    selectOS->hide();
}

FormISO::~FormISO()
{
    delete pop_menu;
    delete ui;
    delete selectOS;
}
void FormISO::on_pushButton_start_make_clicked()
{
    QComboBox* qcb = this->parent()->findChild<QComboBox*>("comboBox_select_device");
    QComboBox* os=this->parent()->findChild<QComboBox*>("comboBox");
    QString image_path=ui->lineEdit_path_iso->text();
    QString os_str= os->currentText();
    HWND windID = (HWND)this->parentWidget()->parentWidget()->winId();
    int boot_type=3;
    long deviceNum;
    QVariant qv = qcb->currentData();
    deviceNum = qv.toInt();
    if (deviceNum < 0)
    {
        QMessageBox::information(this, QString::fromLocal8Bit("消息提醒"), QString::fromLocal8Bit("请插入一张U盘"), QMessageBox::Ok);
        return;
    }
    if (image_path.length() <= 0)
    {
        QMessageBox::information(this, QString::fromLocal8Bit("消息提醒"), QString::fromLocal8Bit("请选择镜像文件"), QMessageBox::Ok);
        return;
    }
    QByteArray ba2;
    ba2.append(image_path.toLocal8Bit());
    char* path;
    QTextCodec* utf8 = QTextCodec::codecForName("UTF-8");
    QByteArray utf8_bytes = utf8->fromUnicode(image_path);
    path = utf8_bytes.data();//urf-8编码看看能不能行，还真能行
    device.startFormat(boot_type, deviceNum,path, windID);
}
void FormISO::remove_selected_action_clicked()
{
    QComboBox* qcb = this->parent()->findChild<QComboBox*>("comboBox_select_device");
    long deviceNum;
    QVariant qv = qcb->currentData();
    deviceNum = qv.toInt();
    if (deviceNum < 0)
    {
        QMessageBox::information(this, QString::fromLocal8Bit("消息提醒"), QString::fromLocal8Bit("请插入一张U盘"), QMessageBox::Ok);
        return;
    }
    HWND windID = (HWND)this->parentWidget()->parentWidget()->winId();
    //HWND windID = (HWND)widget->winId();//获取当前this窗口的句柄
    //获取应用程序的路径
    QString curPath = QDir::currentPath();//获取系统当前目录
    QString dlgTitle = QString::fromLocal8Bit("选择多个文件"); //对话框标题
    QString filter = QString::fromLocal8Bit("镜像文件(*.iso)"); //文件过滤器
    QStringList fileList = QFileDialog::getOpenFileNames(this, dlgTitle, curPath, filter);
    if (fileList.size()<=0)
        return;
    ui->lineEdit_path_iso->setText(fileList.at(0));
    QString image_path1 = ui->lineEdit_path_iso->text();
    QByteArray ba2;
    ba2.append(image_path1.toLocal8Bit());
    char* path1;
    QTextCodec* utf8 = QTextCodec::codecForName("UTF-8");
    QByteArray utf8_bytes = utf8->fromUnicode(image_path1);
    path1 = utf8_bytes.data();//urf-8编码看看能不能行，还真能行
    device.checkISO(path1,windID);
    
}
void FormISO::add_folder_action_clicked()
{
    ////记得回显
    this->hide();
    QPushButton* qw = this->parent()->findChild<QPushButton*>("pushButton_ModeDefault");
    qw->hide();
    QPushButton* qw1 = this->parent()->findChild<QPushButton*>("pushButton_ModeISO");
    qw1->hide();
    selectOS->resize(700, 295);
    selectOS->move(0, 165);
    selectOS->show();
}
